package com.youbug.mall.service_ucs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.common.vo.R;
import com.youbug.mall.service_ucs.entity.UcsChat;
import com.youbug.mall.service_ucs.entity.UcsMessage;
import com.youbug.mall.service_ucs.entity.UcsServiceTicket;
import com.youbug.mall.service_ucs.service.IChatService;
import com.youbug.mall.service_ucs.service.IMessageService;
import com.youbug.mall.service_ucs.service.IServiceTicketService;
import com.youbug.mall.service_ucs.vo.UcsChatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会话表 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-01-07
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private IChatService chatService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IServiceTicketService serviceTicketService;

    @PostMapping("/getChats")
    public R<IPage<UcsChat>> getChats(@RequestBody UcsChatVo chatVo){

        IPage<UcsChat> chats = chatService.getChatPage(chatVo);

        return R.ok(chats);
    }

    @GetMapping("/getChatMessages/{chatId}")
    public R<List<UcsMessage>> getChatMessages(@PathVariable Long chatId){

        QueryWrapper<UcsMessage> wrapper = new QueryWrapper <> ();

        wrapper.eq("chat_id", chatId);
        wrapper.orderByAsc("create_time");
        List<UcsMessage> ucsMessages = messageService.list(wrapper);

        return R.ok(ucsMessages);
    }

    @GetMapping("/getTicket/{chatId}")
    public R<UcsServiceTicket> getTicket(@PathVariable Long chatId){

        QueryWrapper<UcsServiceTicket> wrapper = new QueryWrapper <> ();

        wrapper.eq("chat_id", chatId);

        UcsServiceTicket ticket = serviceTicketService.getOne(wrapper);

        return R.ok(ticket);
    }

    @PostMapping("/download")
    public R<String> download(@RequestBody UcsChatVo chatVo){

        String fileUrl = chatService.download(chatVo);

        return R.ok(fileUrl);
    }
}

